package cn.itcast.itcaststore.web.servlet.client;

import cn.itcast.itcaststore.domain.Products;
import cn.itcast.itcaststore.exception.FindProductByIdException;
import cn.itcast.itcaststore.serive.ProductService;
import cn.itcast.itcaststore.serive.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/*
* 添加商品操作
*
* */
@WebServlet("/addCart")
public class AddCartServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.得到商品id
        String id = request.getParameter("id");
        // 2.调用service层方法，根据id查找商品
        ProductService service = new ProductServiceImpl();
        try {
            Products p = service.findProductById(id);
            //3.将商品添加到购物车
            //3.1获得session对象
            HttpSession session = request.getSession();
            //3.2从session中获取购物车对象
            Map<Products, Integer> cart = (Map<Products, Integer>)session.getAttribute("cart");
            //3.3如果购物车为null,说明没有商品存储在购物车中，创建出购物车
            if (cart == null) {
                cart = new HashMap<Products, Integer>();
            }
            //3.4向购物车中添加商品
            Integer count = cart.put(p, 1);
            //3.5如果商品数量不为空，则商品数量+1，否则添加新的商品信息
            if (count != null) {
                cart.put(p, count + 1);
            }
            session.setAttribute("cart", cart);
            response.sendRedirect(request.getContextPath() + "/client/cart.jsp");
            return;
        } catch (FindProductByIdException e) {
            e.printStackTrace();
        }
    }
}
